package com.ruoyi.tranShort.service.impl;

import java.util.List;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.serialPort.api.SerialPortClientApi;
import com.ruoyi.tranShort.mqtt.Subscribe;
import com.ruoyi.tranShort.mqtt.api.WristletWriteBackCallBack;
import com.ruoyi.tranShort.serialPort.MySerialPortCallBack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tranShort.mapper.ShortSerialPortConfigMapper;
import com.ruoyi.tranShort.domain.ShortSerialPortConfig;
import com.ruoyi.tranShort.service.IShortSerialPortConfigService;

/**
 * 串口配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-14
 */
@Service
public class ShortSerialPortConfigServiceImpl implements IShortSerialPortConfigService 
{
    @Autowired
    private ShortSerialPortConfigMapper shortSerialPortConfigMapper;

    @Autowired
    private WristletWriteBackCallBack wristletWriteBackCallBack;

    public static SerialPortClientApi serialPortClientApi;

    /**
     * 查询串口配置
     * 
     * @param id 串口配置主键
     * @return 串口配置
     */
    @Override
    public ShortSerialPortConfig selectShortSerialPortConfig()
    {
        return shortSerialPortConfigMapper.selectShortSerialPortConfig();
    }



    /**
     * 连接串口
     * 
     * @param shortSerialPortConfig 串口配置
     * @return 结果
     */
    @Override
    public int updateShortSerialPortConfig(ShortSerialPortConfig shortSerialPortConfig)
    {
        if(StringUtils.isEmpty(shortSerialPortConfig.getPort())){
            throw new CustomException("参数错误", HttpStatus.BAD_REQUEST);
        }
        if(serialPortClientApi != null){
            //进行关闭
        }
        int connect = this.connect(shortSerialPortConfig);
        int i = shortSerialPortConfigMapper.updateShortSerialPortConfig(shortSerialPortConfig);
        return connect;
    }

    /**
     * 连接串口
     * @param shortSerialPortConfig
     * @return
     */
    @Override
    public int connect(ShortSerialPortConfig shortSerialPortConfig){
        //初始化
        serialPortClientApi = new SerialPortClientApi(shortSerialPortConfig.getPort());
        int init = serialPortClientApi.init();
        MySerialPortCallBack mySerialPortCallBack = new MySerialPortCallBack();
        //设置回调函数
        serialPortClientApi.setCallBack(mySerialPortCallBack);

        //设置叫哨回复回调
        MySerialPortCallBack.wristletWriteBackCallBack = wristletWriteBackCallBack;
        return init;
    }

    /**
     * 获取串口列表
     * @return
     */
    @Override
    public AjaxResult getPortAll() {
        List<String> portList = SerialPortClientApi.getPortList();
        return AjaxResult.success(portList);
    }

    /**
     * 关闭
     * @return
     */
    @Override
    public AjaxResult close() {
        serialPortClientApi.close();
        return AjaxResult.success();
    }

}
